home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_tile.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  191 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_tile.c,v 1.12 92/10/21 13:42:20 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * Tiled Image Support Routines.
  33.  */
  34. #include "tiffiop.h"
  35.  
  36. /*
  37.  * Compute which tile an (x,y,z,s) value is in.
  38.  */
  39. u_int
  40. DECLARE5(TIFFComputeTile, TIFF*, tif, u_long, x, u_long, y, u_long, z, u_int, s)
  41. {
  42.     TIFFDirectory *td = &tif->tif_dir;
  43.     u_long dx = td->td_tilewidth;
  44.     u_long dy = td->td_tilelength;
  45.     u_long dz = td->td_tiledepth;
  46.     u_int tile = 1;
  47.  
  48.     if (td->td_imagedepth == 1)
  49.         z = 0;
  50.     if (dx == (u_long) -1)
  51.         dx = td->td_imagewidth;
  52.     if (dy == (u_long) -1)
  53.         dy = td->td_imagelength;
  54.     if (dz == (u_long) -1)
  55.         dz = td->td_imagedepth;
  56.     if (dx != 0 && dy != 0 && dz != 0) {
  57.         u_int xpt = howmany(td->td_imagewidth, dx); 
  58.         u_int ypt = howmany(td->td_imagelength, dy); 
  59.         u_int zpt = howmany(td->td_imagedepth, dz); 
  60.  
  61.         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 
  62.             tile = (xpt*ypt*zpt)*s +
  63.                  (xpt*ypt)*(z/dz) +
  64.                  xpt*(y/dy) +
  65.                  x/dx;
  66.         else
  67.             tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx + s;
  68.     }
  69.     return (tile);
  70. }
  71.  
  72. /*
  73.  * Check an (x,y,z,s) coordinate
  74.  * against the image bounds.
  75.  */
  76. DECLARE5(TIFFCheckTile, TIFF*, tif, u_long, x, u_long, y, u_long, z, u_int, s)
  77. {
  78.     TIFFDirectory *td = &tif->tif_dir;
  79.  
  80.     if (x >= td->td_imagewidth) {
  81.         TIFFError(tif->tif_name, "Col %d out of range, max %d",
  82.             x, td->td_imagewidth);
  83.         return (0);
  84.     }
  85.     if (y >= td->td_imagelength) {
  86.         TIFFError(tif->tif_name, "Row %d out of range, max %d",
  87.             y, td->td_imagelength);
  88.         return (0);
  89.     }
  90.     if (z >= td->td_imagedepth) {
  91.         TIFFError(tif->tif_name, "Depth %d out of range, max %d",
  92.             z, td->td_imagedepth);
  93.         return (0);
  94.     }
  95.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
  96.         s >= td->td_samplesperpixel) {
  97.         TIFFError(tif->tif_name, "Sample %d out of range, max %d",
  98.             s, td->td_samplesperpixel);
  99.         return (0);
  100.     }
  101.     return (1);
  102. }
  103.  
  104. /*
  105.  * Compute how many tiles are in an image.
  106.  */
  107. u_int
  108. DECLARE1(TIFFNumberOfTiles, TIFF*, tif)
  109. {
  110.     TIFFDirectory *td = &tif->tif_dir;
  111.     u_long dx = td->td_tilewidth;
  112.     u_long dy = td->td_tilelength;
  113.     u_long dz = td->td_tiledepth;
  114.     u_int ntiles;
  115.  
  116.     if (dx == (u_long) -1)
  117.         dx = td->td_imagewidth;
  118.     if (dy == (u_long) -1)
  119.         dy = td->td_imagelength;
  120.     if (dz == (u_long) -1)
  121.         dz = td->td_imagedepth;
  122.     ntiles = (dx != 0 && dy != 0 && dz != 0) ?
  123.         (howmany(td->td_imagewidth, dx) * howmany(td->td_imagelength, dy) *
  124.         howmany(td->td_imagedepth, dz)) :
  125.         0;
  126.     return (ntiles);
  127. }
  128.  
  129. /*
  130.  * Compute the # bytes in each row of a tile.
  131.  */
  132. u_long
  133. DECLARE1(TIFFTileRowSize, TIFF*, tif)
  134. {
  135.     TIFFDirectory *td = &tif->tif_dir;
  136.     u_long rowsize;
  137.     
  138.     if (td->td_tilelength == 0 || td->td_tilewidth == 0)
  139.         return (0);
  140.     rowsize = td->td_bitspersample * td->td_tilewidth;
  141.     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  142.         rowsize *= td->td_samplesperpixel;
  143.     return (howmany(rowsize, 8));
  144. }
  145.  
  146. /*
  147.  * Compute the # bytes in a variable length, row-aligned tile.
  148.  */
  149. u_long
  150. DECLARE2(TIFFVTileSize, TIFF*, tif, u_long, nrows)
  151. {
  152.     TIFFDirectory *td = &tif->tif_dir;
  153.     u_long tilesize;
  154.  
  155.     if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
  156.         td->td_tiledepth == 0)
  157.         return (0);
  158. #ifdef YCBCR_SUPPORT
  159.     if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  160.         td->td_photometric == PHOTOMETRIC_YCBCR) {
  161.         /*
  162.          * Packed YCbCr data contain one Cb+Cr for every
  163.          * HorizontalSampling*VerticalSampling Y values.
  164.          * Must also roundup width and height when calculating
  165.          * since images that are not a multiple of the
  166.          * horizontal/vertical subsampling area include
  167.          * YCbCr data for the extended image.
  168.          */
  169.         u_long w =
  170.             roundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
  171.         u_long rowsize = howmany(w*td->td_bitspersample, 8);
  172.         u_long samplingarea =
  173.             td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  174.         nrows = roundup(nrows, td->td_ycbcrsubsampling[1]);
  175.         /* NB: don't need howmany here 'cuz everything is rounded */
  176.         tilesize = nrows*rowsize + 2*(nrows*rowsize / samplingarea);
  177.     } else
  178. #endif
  179.         tilesize = nrows * TIFFTileRowSize(tif);
  180.     return (tilesize * td->td_tiledepth);
  181. }
  182.  
  183. /*
  184.  * Compute the # bytes in a row-aligned tile.
  185.  */
  186. u_long
  187. DECLARE1(TIFFTileSize, TIFF*, tif)
  188. {
  189.     return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
  190. }
  191.